package com.hzhc.controller.vehicle;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CarInfo;
import com.hzhc.entity.VehicleReservation;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.*;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.*;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *
 *  @author zhoumx
 *   @since 2024-06-12
 * (一站式) 车辆预约
 */
@RestController
@RequestMapping("/vehicleReservation")
@RequiredArgsConstructor
public class VehicleReservationController extends BaseController {

    private final VehicleReservationService vehicleReservationService;
    private final CarInfoService carInfoService;
    private final YwOrderService ywOrderService;
    private final YwOrderCntrService ywOrderCntrService;
    private final CntrSizeService sizeService;

    @Value("${file.cntrUploadFolder}")
    private String cntrUploadFolder;
    @Value("${file.cntrAccessPath}")
    private String cntrAccessPath;

    /**
     * 列表查询
     */
    @RequestMapping("getYzsVehicleReservationList")
    public ResultUtil<List<VehicleReservationVo>> getVehicleReservationList(HttpServletRequest request, @RequestBody VehicleReservationVo vehicleReservationVo){
        UserDto user = TokenUtil.getUserMember(request);
        vehicleReservationVo.setMemberId(user.getId());
        vehicleReservationVo.setUserName(user.getRealname());
        vehicleReservationVo.setCompanyId(user.getCompanyId());
        Page<VehicleReservationVo> page = new Page<>(vehicleReservationVo.getPageIndex(), vehicleReservationVo.getPageSize());
        List<VehicleReservationVo> vos = vehicleReservationService.getWxList(page, vehicleReservationVo);
        return ResultUtil.build(vos, page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 新增
     */
    @RequestMapping("/addVehicleReservation")
    @SysTemLog(operModul = "YZS新增车辆预约信息", operType = "新增", operDesc = "YZS新增车辆预约信息", modular = "YZS")
    public ResultUtil<T> addVehicleReservation(HttpServletRequest request,@RequestBody VehicleReservationVo vehicleReservationVo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        vehicleReservationVo.setMemberId(userMember.getId());
        vehicleReservationVo.setUserName(userMember.getRealname());
        vehicleReservationVo.setCompanyId(userMember.getCompanyId());
        return vehicleReservationService.addVehicleReservation(vehicleReservationVo,null,null,cntrUploadFolder,cntrAccessPath);
    }
    /**
     * 修改重进车辆预约（重进、提重、空进）
     */
    @RequestMapping("/updateVehicleReservation")
    @SysTemLog(operModul = "YZS修改车辆预约息", operType = "修改", operDesc = "YZS修改车辆预约息", modular = "YZS")
    public ResultUtil<T> updateVehicleReservation(HttpServletRequest request,@RequestBody VehicleReservationVo vehicleReservationVo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        vehicleReservationVo.setUpdateMemberId(userMember.getId());
        vehicleReservationVo.setUpdateUserName(userMember.getRealname());
        vehicleReservationVo.setCompanyId(userMember.getCompanyId());
        return vehicleReservationService.updateVehicleReservation(vehicleReservationVo,null,null,cntrUploadFolder,cntrAccessPath);
    }
    /**
     * 删除重进车辆预约
     */
    @RequestMapping("/delVehicleReservation")
    @SysTemLog(operModul = "YZS删除重进车辆预约信息", operType = "删除", operDesc = "YZS删除重进车辆预约信息", modular = "YZS")
    public ResultUtil<T> delVehicleReservation(@RequestBody VehicleReservation vo) {
        return vehicleReservationService.delVehicleReservation(vo);
    }

    /**
     * 保存提空重回车辆预约(散改集)
     */
    @RequestMapping("/addTkzhVehicleReservation")
    @SysTemLog(operModul = "YZS新增提空重回车辆预约信息", operType = "新增", operDesc = "YZS新增提空重回车辆预约信息", modular = "YZS")
    public ResultUtil<T> addTkzhVehicleReservation(HttpServletRequest request,@RequestBody VehicleReservationVo vo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        vo.setUpdateMemberId(userMember.getId());
        vo.setUpdateUserName(userMember.getRealname());
        vo.setCompanyId(userMember.getCompanyId());
        return vehicleReservationService.addTkzhVehicleReservation(vo);
    }
    /**
     * 修改提空重回车辆预约(散改集（提空）)
     */
    @RequestMapping("/updateTkzhVehicleReservation")
    @SysTemLog(operModul = "YZS修改提空重回车辆预约息", operType = "修改", operDesc = "YZS修改提空重回车辆预约息", modular = "YZS")
    public ResultUtil<T> updateTkzhVehicleReservation(HttpServletRequest request,@RequestBody VehicleReservationVo vo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        vo.setUpdateMemberId(userMember.getId());
        vo.setUpdateUserName(userMember.getRealname());
        vo.setCompanyId(userMember.getCompanyId());
        return vehicleReservationService.updateTkzhVehicleReservation(vo);
    }

    /**
     * 删除提空重回车辆预约(散改集)
     */
    @RequestMapping("/delTkzhVehicleReservation")
    @SysTemLog(operModul = "YZS删除提空重回车辆预约信息", operType = "删除", operDesc = "YZS删除提空重回车辆预约信息", modular = "YZS")
    public ResultUtil<T> delTkzhVehicleReservation(HttpServletRequest request,@RequestBody VehicleReservation vo) {
        return vehicleReservationService.delTkzhVehicleReservation(vo);
    }


    /**
     * 获取已审核委托接口
     *
     * @param vo
     * @return
     */
    @RequestMapping("/getYwOrderVoJson")
    public ResultUtil<List<YwOrderVo>> getYwOrderVoJson(@RequestBody YwOrderVo vo) {
        Page<YwOrderVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderVo> ywOrderVos = ywOrderService.getYwOrderVoJson(page, vo);
        return ResultUtil.build(ywOrderVos,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 车辆信息
     */
    @RequestMapping("/getCarJsonPage")
    public ResultUtil<List<CarInfoVo>> getCarJsonPage(@RequestBody CarInfoVo vo) {
        Page<CarInfoVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<CarInfoVo> carInfoList = carInfoService.getYZSCarJsonPage(vo, page);
        return ResultUtil.build(carInfoList,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    //yzs add车辆预约  车牌下拉框
    @RequestMapping("/getCarListByVeh")
    public ResultUtil<List<CarInfoVo>> getCarListByVeh(@RequestBody CarInfoVo vo) {
        List<CarInfoVo> carInfoList = carInfoService.getCarListByVeh(vo);
        return ResultUtil.build(carInfoList, ResultUtilCodeEnum.SUCCESS);
    }



    //车辆黑名单校验
    @RequestMapping("/checkIsBlack")
    public ResultUtil<T> checkIsBlack(@RequestBody CarInfo carInfo){
        return carInfoService.checkIsBlack(carInfo);
    }

    /**
     * 获取委托箱子信息
     *
     * @param vo
     * @return
     */
    @RequestMapping("/getYwOrderCntrVoJson")
    public ResultUtil<List<YwOrderCntrVo>> getYwOrderVoJson(@RequestBody YwOrderCntrVo vo) {
        Page<YwOrderCntrVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderCntrVo> ywOrderVos = ywOrderCntrService.getYwOrderCntrVoJson(page, vo);
        return ResultUtil.build(ywOrderVos,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     *  箱尺寸
     */
    @RequestMapping("/getCntrSizeTypeList")
    public ResultUtil<List<CntrSizeVo>> getCntrSizeTypeList(HttpServletRequest request, @RequestBody CntrSizeVo vo){
        List<CntrSizeVo> list = sizeService.getCntrSizeTypeList(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 修改车牌号
     */
    @RequestMapping("/updateCarNo")
    @SysTemLog(operModul = "车辆预约-修改车牌号", operType = "修改", operDesc = "YZS车辆预约-修改车牌号", modular = "YZS")
    public ResultUtil<T> updateCarNo(HttpServletRequest request,@RequestBody VehicleReservationVo vo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        vo.setUpdateMemberId(userMember.getId());
        vo.setUpdateUserName(userMember.getRealname());
        return vehicleReservationService.updateCarNo(vo);
    }

    /**
     * 预约导入
     * 保存上传
     */
    @PostMapping("/importYZSYuyueList")
    public ResultUtil<T> importYuyueList(HttpServletRequest request, @RequestBody VehicleReservationVo vo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        vo.setMemberId(userMember.getId());
        vo.setUserName(userMember.getRealname());
        vo.setCompanyId(userMember.getCompanyId());
        vo.setCompanyName(userMember.getCompanyName());
        return vehicleReservationService.importYZSYuyueList(request, vo);
    }
    /**
     * 导出模板下载
     */
    private final static String EXPORT_EXCEL_PATH_YZS = "static/download/预约车辆.xlsx";

    @GetMapping("/exportYZSYuyueFile")
    public void exportYuyueFile(HttpServletRequest request, HttpServletResponse response) {
        vehicleReservationService.exportYZSYuyueFile(request, response, EXPORT_EXCEL_PATH_YZS);
    }

    /**
     *导入上传
     */
    @RequestMapping("/yuyueYZSUpload/{orderNo}/{companyId}")
    public ResultUtil<List<VehicleReservationVo>> YuyueUpload(MultipartFile file, HttpServletRequest request,@PathVariable String orderNo,@PathVariable String companyId) {
        UserDto user = TokenUtil.getUser(request);
        return vehicleReservationService.YuyueYZSUpload(user,file,orderNo,companyId);
    }


}
